<?php
namespace Shopadmin\Model;
use Think\Model;
class UsersGiftsModel extends Model {
	public $usernick;
	public $uzuid;
	public $sid;
	public $limit;
	public $where = "1";
	public $isfront = false;
	public function GetTotal(){
		$where = $this->where;
		$where .= $this->uzuid?" AND `sm_users_gifts`.`uzuid`=".$this->uzuid:"";
		$where .= $this->usernick?" AND `sm_user`.`usernick`='".$this->usernick."'":"";
		$where .= $this->sid?" AND `sm_events_gifts`.`sid`=".$this->sid:"";
		$join = $this->usernick?"LEFT JOIN `sm_shop_users` ON `sm_shop_users`.id = __USERS_GIFTS__.uzuid LEFT JOIN `sm_user` ON `sm_user`.id = sm_shop_users.userid":"";
		$this->total = $this->where($where)->join("LEFT JOIN __EVENTS_GIFTS__ ON __USERS_GIFTS__.giftid = __EVENTS_GIFTS__.id ".$join)->getField("count(`sm_users_gifts`.`id`)");
		return $this->total;
	}
	public function JoinList(){
		$where = $this->where;
		$where .= $this->uzuid?" AND `sm_users_gifts`.`uzuid`=".$this->uzuid:"";
		$where .= $this->sid?" AND `sm_events_gifts`.`sid`=".$this->sid:"";
		$data = $this->join("LEFT JOIN __EVENTS__ ON __USERS_GIFTS__.eventid = __EVENTS__.id")
					 ->join("LEFT JOIN __EVENTS_GIFTS__ ON __USERS_GIFTS__.giftid = __EVENTS_GIFTS__.id")
					 ->where($where)
					 ->order("`sm_events_gifts`.`id` desc")
					 ->limit($this->limit)
					 ->getField("`sm_events_gifts`.`id`,
								 `sm_events_gifts`.`id`,
								 `sm_events_gifts`.`type`,
								 `sm_events_gifts`.`level`,
								 `sm_events_gifts`.`name`,
								 `sm_events_gifts`.`isshow`,
								 `sm_events_gifts`.`quantity`,
								 `sm_events_gifts`.`event_id`,
								 `sm_events`.`templet_code`,
								 `sm_events`.`title`,
								 `sm_events`.`start_time`,
								 `sm_events`.`end_time`,
								 `sm_events`.`id` AS `event_id`,
								 `sm_users_gifts`.`status`,
								 `sm_users_gifts`.`tradeid`,
								 `sm_users_gifts`.`id` AS `gid`
								 ");
		return $data;
	}
	public function SendList(){
		$where = $this->where;
		$where .= $this->uzuid?" AND `sm_users_gifts`.`uzuid`=".$this->uzuid:"";
		$where .= $this->usernick?" AND `sm_user`.`usernick`='".$this->usernick."'":"";
		$where .= $this->sid?" AND `sm_events_gifts`.`sid`=".$this->sid:"";
		$where .= $this->isfront?" AND `sm_events_gifts`.`level`<>6":"";
		// $this->query("SET @rowNum =0");
		$field=$this->field?$this->field:" `sm_users_gifts`.`id`,
				 `sm_events_gifts`.`type`,
				 `sm_events_gifts`.`level`,
				 `sm_events_gifts`.`price`,
				 `sm_events_gifts`.`name`,
				 `sm_events_gifts`.`isshow`,
				 `sm_events_gifts`.`quantity`,
				 `sm_events_gifts`.`event_id`,
				 `sm_events_gifts`.`sid`,
				 `sm_users_gifts`.`status`,
				 `sm_users_gifts`.`uzuid`,
				 `sm_users_gifts`.`giftid`,
				 `sm_users_gifts`.`quantity` AS `ugq`,
				 `sm_users_gifts`.`actiontime`,
				 `sm_users_gifts`.`getway`,
				 `sm_user`.`usernick`,
				 `sm_shop_users`.`addtime`,
				 `sm_shop_users`.`id` as `shopuid`
				 ";
		$data = $this->join("LEFT JOIN __EVENTS_GIFTS__ ON __EVENTS_GIFTS__.id = __USERS_GIFTS__.giftid")
					 ->join("LEFT JOIN `sm_shop_users` ON `sm_shop_users`.id = __USERS_GIFTS__.uzuid") //, (SELECT @rowNum :=0) t
					 ->join("LEFT JOIN `sm_user` ON `sm_user`.id = sm_shop_users.userid") //, (SELECT @rowNum :=0) t
					 ->where($where)
					 ->order("`sm_users_gifts`.`id` desc")
					 ->limit($this->limit)
					 // ->group("`sm_users_gifts`.`uzuid`,`sm_events_gifts`.`id`") // (@rowNum:=@rowNum+1) as rowid,
					 ->getField($field);
		return $data;
	}
	public function LingJiang(){
		$where = $this->where;
		$where .= $this->uzuid?" AND `sm_users_gifts`.`uzuid`=".$this->uzuid:"";
		$where .= $this->gid?" AND `sm_users_gifts`.`id`=".$this->gid:"";
		$data = $this->join("LEFT JOIN __EVENTS_GIFTS__ ON __USERS_GIFTS__.giftid = __EVENTS_GIFTS__.id")
					 ->where($where)
					 ->limit($this->limit)
					 ->getField("`sm_events_gifts`.`id`,
								 `sm_events_gifts`.`name` AS giftname,
								 `sm_events_gifts`.`pic_url` AS giftpic,
								 `sm_events_gifts`.`num_iids` AS withdraw,
								 `sm_events_gifts`.`type`,
								 `sm_users_gifts`.`status`
								 ");
		return $data;
	}
	public function CertiFicat(){
		$this->where = "`sm_users_gifts`.`id`=".$this->gid;
		$this->field = "`sm_events_gifts`.`sid`,
						`sm_events_gifts`.`id`,
						`sm_events_gifts`.`type`,
						`sm_events_gifts`.`level`,
						`sm_events_gifts`.`name`,
						`sm_events_gifts`.`event_id`,
						`sm_events_gifts`.`details`,
						`sm_events_gifts`.`num_iids`,
						`sm_events`.`templet_code`,
						`sm_events`.`title`,
						`sm_events`.`picurl` AS `event_pic`,
						`sm_events`.`start_time`,
						`sm_events`.`end_time`,
						`sm_events`.`withdraw`,
						`sm_events`.`discription`,
						`sm_events`.`id` AS `event_id`,
						`sm_user`.`id` AS `uzuid`,
						`sm_user`.`mixednick`,
						`sm_user`.`usernick`,
						`sm_users_gifts`.`actiontime`,
						`sm_events_gifts`.`pic_url`
						";
		$re = current($this->SendList());
		if($re){
			$data["gid"]=$this->gid;
			$data["create_time"]=date("Y-m-d H:i:s",time());
			$data["gift_name"]=$re["name"];
			$data["gift_description"]=$re["details"];
			$data["gift_link"]=$re["num_iids"];
			$data["gift_pic"]=$re["pic_url"];
			$data["gift_lev"]=$re["level"];
			$data["gift_type"]=$re["type"];
			$data["gift_actime"]=$re["actiontime"];
			$data["mixednick"]=$re["mixednick"];
			$data["truename"]=$re["usernick"];
			$data["event_id"]=$re["event_id"];
			$data["event_name"]=$re["title"];
			$data["event_pic"]=$re["event_pic"];
			$data["event_description"]=$re["discription"];
			$data["event_start_time"]=$re["start_time"];
			$data["event_end_time"]=$re["end_time"];
			$data["event_with_draw_time"]=$re["withdraw"];
			$data["sid"]=$re["sid"];
			$data["uzuid"]=$re["uzuid"];
			$Shop = M("Shop");
			$shop = current($Shop->where("`sid`=".$re["sid"])->getField("usernick,shoptitle,avatar"));
			if($shop){
			$data["shopname"]=$shop["shoptitle"];
			$data["wangwang"]=$shop["usernick"];
			}
			$C = M("Certification");
			$C->add($data);
			return $data;
		}else{
			return false;
		}
	}
	public function send_gift(){
		// $getway 获奖途径 jifen：积分兑换 chouj：抽奖中奖
		// $usernick 要发奖的昵称
		// $giftid = 奖品ID
		// $gifttp = 奖品类型，数字
		// $gifttagid = 折扣奖品的标签id
		// $UserSPID = 店铺用户id;
		
		if($this->gifttp==7){
			// 再来一次奖品的处理
			$GFT = M("EventsGifts");
			$score_ingift = $GFT->where("`id`=".$this->giftid)->getField("num_iids"); //取出要增加的次数
			$U = D("EventsUsers");
			$Uscore = $U->where("`uzuid`='".$this->UserSPID."' AND `sid`=".$this->sid." AND `eventid`=0")->limit(1)->getField("id");//echo $U->_sql();
			if(!$Uscore){
				// 用户没有参与抽奖时要增加首次参与次数
				$Shop = M("ShopCfg");
				$tms_newcomer = $Shop->where("`sid`=".$this->sid)->getField("tms_newcomer");
				$us["score"] = $tms_newcomer+$score_ingift;
				$us["uzuid"] = $this->UserSPID;
				$us["sid"] = $this->sid;
				$us["addtime"]=date("Y-m-d H:i:s",time());
				$us["likeme"]=0;
				$us["shareme"]=0;
				$us["collect"]=0;
				$us["followme"]=0;
				$Uscore = $U->add($us);
			}else{
				// 用户已经参与抽奖则直接增加次数
				$this->execute("update `sm_events_users` set `score` = `score`+".$score_ingift." WHERE `id` = ".$Uscore);
			}
		}
		if($this->gifttp==4){
			// 中得卡密要保存卡密id，不论是否已中奖都新增
			$KM = M("EventsGiftsKm");
			$kamiid = $KM->where("`giftid`=".$this->giftid." AND `sid`=".$this->sid." AND `status`=8")->limit(1)->getField("`id`");
			if($kamiid){
				$data["kamiid"]=$kamiid;
				// $data["cardno"]=$kami["cardno"];
				// $data["secret"]=$kami["secret"];
				$kmsv["status"]=9;
				$kmsv["shopuid"]=$this->UserSPID;
				$KM->where("`id`=".$kamiid)->save($kmsv);
			}else{
				$this->gifts[$re]["cardno"]="没有卡号";
				$this->gifts[$re]["secret"]="";
			}
		}
		if($this->gifttp==5){
			// 中得折扣类型奖品，远程加载贴标签动作
			$tagid = $this->gifttagid;
			$ts = time();
			$sign=md5(C('TOOL_KEY').$ts.urlencode_utf8($this->usernick).$tagid.$this->sid.$action.C('TOOL_SECRET')); 
			$r = $fetchService->fetchByGet("http://hg.itianshan.com/?m=Shopmodule&a=adduser&shopid=".$this->sid."&usernick=".urlencode_utf8($this->usernick)."&tagid=".$tagid."&ts=".$ts."&sign=".$sign);
		}
		// if(!$this->ugid){ 
		// 不论是否已有该奖品都新增一个奖品
			$data = array();
			$data["uzuid"]=$this->UserSPID;
			$data["giftid"]=$this->giftid;
			$data["sid"]=$this->sid;
			$data["getway"]=$this->getway;
			$data["actiontime"]=date("Y-m-d H:i:s",time());
			
			if($this->gifttp<>1){
				$data["status"]=1;
			}
			$reda["ugid"] = $this->add($data);
			// $reda["sql"] = $this->_sql();
			// $reda["err"] = $this->getDbError();
			$reda["quant"] = $this->execute("update `sm_events_gifts` set `quantity` = IF(`quantity`<1, 0, `quantity`-1) WHERE `id` = ".$this->giftid);
			if($reda["ugid"]&&$reda["quant"]){
				$reda["status"]=1;
			}else{
				$reda["status"]=0;
			}
			$this->status = $reda;
		// }
	}
}